<template>
  <div class="user">
    <el-card>
      <div class="conter">
        <el-table :data="tableData" border stripe>
          <el-table-column
            prop="loginName"
            label="登录账号"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="qq"
            label="用户QQ号"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="nickname"
            label="用户昵称"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column prop="sex" label="用户性别" width="" align="center">
          </el-table-column>
          <el-table-column prop="image" label="用户头像" width="" align="center">
            <template slot-scope="scope">
              <img :src="api + scope.row.userIcon" min-width="80" height="80" />
            </template>
          </el-table-column>
           <el-table-column
            prop="createtime"
            label="注册时间"
            width=""
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" width="80" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="Edituser(scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加弹框 对话框-->
      <el-dialog title="编辑" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="登录账号" :label-width="formLabelWidth">
            <el-input v-model="form.loginName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户QQ号" :label-width="formLabelWidth">
            <el-input v-model="form.qq" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-input v-model="form.sex" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="头像" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              :action="uploadimageapi+'/usericonuploadimage'"
              :show-file-list="false"
              name="usericonfiles"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="Updateuser">提 交</el-button>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      form: {
        loginName: "",
        loginPassword: "",
        nickname: "",
        sex: "",
        userIcon: "",
        qq: ""
      },
      formLabelWidth: "80px",
      dialogFormVisible: false,
      //图片地址拼接 显示图片
      api: this.imageapi,
      uploadimageapi: this.uploadimageapi,//全局定义
      imageUrl: ""
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //加载用户列表
    init() {
      const that = this;
      const id = sessionStorage.getItem("id")
      that.$http
        .post("/selectuserinfo" , {id})
        .then(res => {
          // console.log(res);
          if (res.status == 200) {
            that.tableData = res.data;
          } else {
            that.$message({
              message: "数据请求失败...",
              type: "error"
            });
          }
        })
        .catch(function() {});
    },
    //获取编辑内容
    Edituser(item) {
      this.dialogFormVisible = true;
      this.imageUrl = this.api + item.userIcon;
      this.form = JSON.parse(JSON.stringify(item));
    },
     handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.userIcon = res.path;
    },
    Updateuser() {
      const that = this;
      that.$http
        .post("/updateuser", that.form)
        .then(res => {
          if (res.status == 204) {
            that.$message({
              message: "修改个人资料成功",
              type: "success"
            });
            that.init();
            that.dialogFormVisible = false;
          } else {
            that.$message({
              message: "更新数据失败...",
              type: "error"
            });
          }
          // console.log(res);
          that.dialogFormVisible = false;
          that.form.loginName = null;
          that.form.loginPassword = null;
          that.form.sex = null;
          that.form.nickname = null;
        })
        .catch(function() {});
    },
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-pagination {
  text-align: center;
  margin: 10px 0px 20px 0px;
}
</style>